package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.annotation.SystemServiceLog;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootLevelUp;
import org.footballmanager.pandafootball.system.domain.FootSociaty;
import org.footballmanager.pandafootball.system.form.impl.FootLevelUpSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootLevelUpMapper;
import org.footballmanager.pandafootball.system.service.FootLevelUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootLevelUpServiceImpl extends ServiceImpl<FootLevelUp> implements FootLevelUpService {
    @Autowired
    private FootLevelUpMapper footLevelUpMapper;

    /**
     * 查询
     *
     * @param entityname            实体类名
     * @param map                   橙色
     * @param footLevelUpSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootLevelUpSearchForm footLevelUpSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footLevelUpSearchForm);
        Page<FootLevelUp> page = PageHelper.startPage(footLevelUpSearchForm.getPageIndex(), footLevelUpSearchForm.getPageSize());
        List<FootLevelUp> list = this.findall(footLevelUpSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footLevelUpSearchForm 条件
     * @param request               请求
     */
    @Override
    public void deleteAll(FootLevelUpSearchForm footLevelUpSearchForm, HttpServletRequest request) {
        if(footLevelUpSearchForm.getSelectIds() != null){
            String[] result = footLevelUpSearchForm.getSelectIds().split(Constants.COMMA);
            for(String id: result){
                this .delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 生成等级算法
     */
    @Override
    public void calculateLevel(HttpServletRequest request) {
        int initJf = 5;
        int level = 100;
        int result = 0;
        for(int i = 1; i <= level; i++){
            result += initJf + i;
            initJf += 2;
            FootLevelUp levelUp = new FootLevelUp();
            levelUp.setLevel(i);
            levelUp.setJf(result);
            this.save(levelUp, request);
        }
    }

    /**
     * 计算等级
     *
     * @param footSociaty 实体类
     * @param request     请求
     * @return 等级实体
     */
    @Override
    public FootLevelUp calculateLevel(FootSociaty footSociaty, HttpServletRequest request) {
        Example example = new Example(FootLevelUp.class);
        example.orderBy("level asc");
        List<FootLevelUp> list = this.findall(example);
        if(list != null && !list.isEmpty()){
            for(FootLevelUp footLevelUp: list){
                if (footSociaty.getJf() <= footLevelUp.getJf()){
                    return footLevelUp;
                }
            }
        }
        return null;
    }

    /**
     * 查询
     *
     * @param footLevelUpSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootLevelUp> findall(FootLevelUpSearchForm footLevelUpSearchForm) {
        return footLevelUpMapper.findall(footLevelUpSearchForm);
    }

    /**
     * 根据等级关系主键获取等级关系记录
     *
     * @param id 等级关系主键
     * @return 等级关系记录
     */
    @Override
    @SystemServiceLog(description = "根据等级关系主键获取等级关系记录")
    public FootLevelUp get(Serializable id){
        return footLevelUpMapper.findById(id);
    }
}
